package com.xiangban.volunteer_platform.controller;

import com.xiangban.volunteer_platform.entity.Task;
import com.xiangban.volunteer_platform.service.TaskService;
import com.xiangban.volunteer_platform.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Mr. Ma
 * @Data 2021/1/21 10:24
 */
@RestController
@RequestMapping("api/task")
@Api(tags = "任务发布控制器")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @PostMapping("/saveTask")
    @ApiOperation("任务添加")
    public JsonData saveTask(@RequestBody Task task){
        int i = taskService.saveTask(task);
        if (i==0){
            return JsonData.buildError("操作失败,检查传输信息是否正确！");
        }
        return JsonData.buildSuccess(task.getId());
    }

    @PostMapping("/deleteTask")
    @ApiOperation("任务删除")
    public JsonData deleteTask(@RequestBody Task task){
        int i = taskService.deleteTask(task);
        if (i==-1){
            return JsonData.buildError("操作失败，查无此人");
        }
        return JsonData.buildSuccess("操作成功");
    }
    @PostMapping("/updateTask")
    @ApiOperation("修改任务")
    public JsonData updateTask(@RequestBody Task task){
        int i = taskService.updateTask(task);
        if (i==-1){
            return JsonData.buildError("操作失败！");
        }
        return JsonData.buildSuccess("操作成功！");
    }
    @PostMapping("/getAllTask")
    @ApiOperation("分页获取所有")
    public JsonData getAllTask(@RequestParam(required = false) Integer pageNum,
                               @RequestParam(required = false) Integer pageSize,@RequestBody Task task){
       return JsonData.buildSuccess(taskService.getAllTask(pageNum,pageSize,task));
    }

    @PostMapping("/getTask")
    @ApiOperation("根据id获取任务")
    public JsonData getTask(@RequestBody Task task){
       return JsonData.buildSuccess(taskService.getTaskById(task));
    }

}
